import hashlib
import json
import time
import os


def get_file_split(path):
    '''
    返回文件名与文件后缀
    '''
    return os.path.basename(path),os.path.splitext(path)[-1]

def get_current_time():
    '''
    获取网络时间
    time format:yyyymmddss
    '''
    pass

def encryp_sha1(string,key):
    utf = (string+key).encode('utf-8')
    high = hashlib.sha1(utf)
    high.update(key.encode('utf-8'))
    high_res = high.hexdigest()
    return high_res

def write_100_file(path,str):
    with open(path,'w') as f:
        f.write(str)

def read_encrypt_file(path):
    with open(path, 'rb') as f:
        return f.read(100)

def read_late_encrypt(path):
    with open(path, 'rb') as f:
        return f.read()[100:]

def create_json(timestamp, time_across):
    return json.dumps({'timestamp': timestamp, 'time_across': time_across})


def write_ahead(timestamp, time_crose, source_path, des_path):
    '''
    写入100个字符，但是会覆盖原有内容，所以要先创建文件，写好内容，再接上其他内容
    '''
    string = create_json(timestamp, time_crose)

    if len(string) < 100:
        f = open(source_path, 'wb')
        for m in str(string):
            f.write(m.encode('utf-8'))

        for i in range(100 - len(string)):
            f.write(b'_')

        file = write_append(des_path)
        f.write(file)
        return True
    else:
        print('密码长度错误！')


def write_append(content_path):
    af = open(content_path, 'rb')
    file = af.read()
    af.close()
    return file


def analyse_data(path):
    b_text = read_encrypt_file(path)
    d_text = bytes.decode(b_text)
    # print (d_text)
    res = json.loads(d_text.strip('_'))
    return res


def is_arrive_time(path):
    now_time = time.time()
    res = analyse_data(path)
    if res['timestamp'] + res['time_across'] < now_time:
        return True
    else:
        return False


if __name__ == "__main__":
    # now_time = int(time.time())
    # time_cross = 500
    # key = 'mylove'
    #
    # encryp_sha1(str(now_time)+str(time_cross)+key,key)

    #提示：1小时=3600秒，1天=86400秒，一年=31536000秒。
    #time.ctime(1629798961+157680000)

    des_path = r'./my_encode.zip'
    source_path = r'./secret.lock'

    #write_ahead(int(now_time),time_cross,source_path,des_path)
    #read_res = read_encrypt_file(des_path)
    print (analyse_data(source_path))